package cn.lyvust.eais.manage.controller;


import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.manage.dto.param.SysUserParam;
import cn.lyvust.eais.manage.entity.SysUser;
import cn.lyvust.eais.manage.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ggzheng
 * @since 2021-03-05
 */
@Api(tags = "用户信息")
@RestController
@Controller
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询信息")
    public ResponseResult list(@RequestBody SysUserParam param) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getUsername())) {
            wrapper.eq("username", param.getUsername());
        }
        if (!StringUtils.isEmpty(param.getPassword())) {
            wrapper.eq("password", param.getPassword());
        }
        wrapper.orderByDesc("create_date");
        return ResponseResult.e(ResponseCode.OK, sysUserService.list(wrapper));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改信息")
    @SysLogs("修改信息")
    public ResponseResult update(@RequestBody SysUserParam param) {
        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("修改失败,用户名不能为空!");
        }
        if (StringUtils.isEmpty(param.getPassword())) {
            throw RequestException.fail("修改失败,密码不能为空!");
        }
        SysUser entity = sysUserService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, sysUserService.updateById(entity));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增信息")
    @SysLogs("新增信息")
    public ResponseResult add(@RequestBody SysUserParam param) {
        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("添加失败,用户名不能为空!");
        }
        if (StringUtils.isEmpty(param.getPassword())) {
            throw RequestException.fail("添加失败,密码不能为空!");
        }
        if (StringUtils.isEmpty(param.getAge())) {
            throw RequestException.fail("添加失败,年龄不能为空!");
        }
        if (StringUtils.isEmpty(param.getStatus())) {
            throw RequestException.fail("添加失败,职位不能为空!");
        }
        SysUser entity = new SysUser();
        BeanUtils.copyProperties(param, entity);
        SysUser one = sysUserService.getOne(new QueryWrapper<SysUser>().eq("Username", param.getUsername()));
        if (one != null) {
            throw RequestException.fail("用户名已存在!");
        }
        return ResponseResult.e(ResponseCode.OK, sysUserService.save(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除信息")
    @SysLogs("删除信息")
    public ResponseResult delete(@RequestBody SysUserParam param) {
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("删除失败,ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, sysUserService.removeById(param.getId()));
    }
}
